// index.js
// 获取应用实例
const app = getApp();
Page({
  data: {
    // 18591990935 
    productInfo: [],
    imgUrl: "",
    content: '', 
    picture: '', 
    ReimbursementData: [],
    financeId: '',
    isUpload: false,
    fileLength: 0,
    plaReason: '请输入内容'
  },
  onLoad(options) {
    this.setData({
      financeId: options.id
    });
    wx.hideShareMenu();
  },
  onReady() {
  },
  onShow() {


  },
  //刷新
  onPullDownRefresh() {

  },

  removeImage(e) {
    const arr = this.data.productInfo;
    const index = e.currentTarget.dataset.index
    arr.splice(index, 1);
    this.setData({ productInfo: arr })
  },
  //预览图片，放大预览
  preview(e) {
    let currentUrl = this.data.productInfo;
    let index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: currentUrl[index], // 当前显示图片的http链接
      urls: this.data.productInfo // 需要预览的图片http链接列表
    })
  },
  // 姓名
  handNameChange: function (e) {
    // 取出定义的变量名
    let targetData = e.currentTarget.dataset.modal;
    // 取出定义的变量名
    let currentValue = e.detail.value;
    // 将 input 值赋值给 定义的变量名，可以直接取到在 data 中定义的 变量 值，其效果跟 this[变量名] 是对等的，这是 js 基础
    this[targetData] = currentValue;
    //  console.log(this[targetData]);
    this.setData({
      content: this[targetData]
    })
  },


  // 提交
  appointmentApplication() {
    if (this.data.content == "") {
      wx.showToast({
        title: '请输入内容',
        icon: "none"

      });
    }else if(!this.data.productInfo || this.data.productInfo?.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: "none"
      });
    } else if (this.data.productInfo && this.data.productInfo?.length > 9) {
      wx.showToast({
        title: '最多上传9张图片',
        icon: "none"
      });
    } else {
      let photoStr = this.data.productInfo.join(",");
      let that = this;
      wx.request({
        url: `${app.globalData.baseUrl}/api/notice/saveComment`,
        method: "POST",
        data: {
          financeId: that.data.financeId,
          content: that.data.content,
          picture: photoStr, // 照片最多三张多个逗号隔开
        },
        header: {
          "Content-Type": "application/json",
          'token': wx.getStorageSync('token'),
        },
        success: (res) => {
          wx.hideLoading()
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.msg || '报错啦！',
              duration: 2000
            })

            setTimeout(() => {
              wx.navigateBack({
                delta: 1,
              })
            }, 1000);
          } else if (res.data.code == 500) {
            wx.showToast({
              title: res.data.msg || '报错啦！',
              icon: 'error',//当icon：'none'时，没有图标 只有文字
              duration: 2000
            })
          }
          else {
            wx.showToast({
              title: '系统错误，请重新登录',
            })
          }
        }
      })
    }
  },
  uploadOpen: function (e) {
    this.setData({
      isUpload: true,
      fileLength: this.data.productInfo.length,
      plaReason: ''
    });
  },
  uploadClose: function () {
    this.setData({ 
      isUpload: false,
      plaReason: '请输入内容'
    });
  },
  receivingData: async function (val) {
    this.setData({
      productInfo: [...this.data.productInfo, val.detail]
    });
  }
});